package com.beg.cant.controller;

import com.beg.cant.comment.result.Result;
import com.beg.cant.dto.PageRequest;
import com.beg.cant.dto.PageResponse;
import com.beg.cant.entity.Order;
import com.beg.cant.mapper.OrderMapper;
import com.beg.cant.service.OrderService;
import com.beg.cant.vo.MonthlyStatisticsVO;
import com.beg.cant.vo.OrderStatisticsVO;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.lang.Long;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.List;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;

@RestController
@RequestMapping("/api/orders")
@Tag(name = "订单管理", description = "订单的增删改查接口")
@RequiredArgsConstructor
public class OrderController {
    @Autowired
    private final OrderService orderService;
    @Autowired
    private final OrderMapper orderMapper;

    @PostMapping
    @Operation(summary = "创建订单", description = "创建一个新的订单")
    public Result<String> createOrder(@RequestBody Order order) {
        orderService.save(order);
        return Result.success("订单创建成功");
    }

    @GetMapping("/{id}")
    @Operation(summary = "获取订单", description = "根据ID获取订单信息")
    public Result<Order> getOrder(@Parameter(description = "订单ID") @PathVariable Long id) {
        return Result.success(orderService.getById(id));
    }
    
    @PutMapping
    @Operation(summary = "更新订单", description = "根据ID更新订单信息")
    public Result<String> updateOrder(@RequestBody Order order) {
        orderService.updateById(order);
        return Result.success("订单更新成功");
    }

    @DeleteMapping("/{id}")
    @Operation(summary = "删除订单", description = "根据ID删除订单")
    public Result<String> deleteOrder(@Parameter(description = "订单ID") @PathVariable Long id) {
        orderService.removeById(id);
        return Result.success("订单删除成功");
    }
    
    @GetMapping("/list")
    @Operation(summary = "获取所有订单", description = "分页获取所有订单的列表")
    public Result<PageResponse<Order>> getAllOrders(PageRequest pageRequest) {
        return Result.success(orderService.getOrderList(pageRequest));
    }

    @GetMapping("/statistics/current")
    @Operation(summary = "获取当前收支统计", description = "统计今日和本月的收支情况")
    public Result<OrderStatisticsVO> getCurrentStatistics() {
        return Result.success(orderService.getCurrentStatistics());
    }

    @GetMapping("/statistics/monthly")
    @Operation(summary = "获取近四个月收支统计", description = "统计最近四个月的收支情况")
    public Result<List<MonthlyStatisticsVO>> getMonthlyStatistics() {
        return Result.success(orderService.getMonthlyStatistics());
    }
}
